// CmdDlg.cpp : implementation file
//

#include "stdafx.h"
#include "kquick.h"
#include "CmdDlg.h"
#include "SettingDlg.h"


// CCmdDlg dialog

IMPLEMENT_DYNAMIC(CCmdDlg, CDialog)

CCmdDlg::CCmdDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCmdDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CCmdDlg::~CCmdDlg()
{
}

void CCmdDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CCmdDlg, CDialog)
	ON_BN_CLICKED(IDOK, &CCmdDlg::OnBnClickedOk)
	ON_WM_PAINT()
	ON_WM_DROPFILES()
END_MESSAGE_MAP()


// CCmdDlg message handlers
BOOL CCmdDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	HWND hwnd = ::GetForegroundWindow();
	if (AttachThreadInput(GetWindowThreadProcessId(hwnd, NULL), GetCurrentThreadId(), TRUE) == TRUE) {
		SetForegroundWindow();
		SetFocus();

		AttachThreadInput(GetWindowThreadProcessId(hwnd, NULL), GetCurrentThreadId(), FALSE);
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCmdDlg::OnBnClickedOk()
{
	GetDlgItemText(IDC_COMMAND, m_cmd);
	OnOK();
}

CString& CCmdDlg::GetCmd() {
	return m_cmd;
}

void CCmdDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
	return;
}

void CCmdDlg::OnDropFiles(HDROP hDropInfo)
{
	CDialog::OnDropFiles(hDropInfo);

	int count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
	if (count != 1)
		return;

	ShowWindow(SW_HIDE);

	TCHAR filepath[MAX_PATH] = {0};
	DragQueryFile(hDropInfo, 0, filepath, MAX_PATH - 1);

	CSettingDlg dlg;
	if (dlg.DoModal() == IDOK) {
		CString& cmd = dlg.GetCmd();
		WritePrivateProfileString(L"SHORTCUTS", cmd.GetBuffer(), filepath, L".\\kquick.ini");
	}

	OnCancel();
}
